package logic

import (
	"context"
	"errors"
	"video-manager/model/mysql"

	"video-manager/usersrpc/internal/svc"
	"video-manager/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddVideoLogic {
	return &AddVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddVideoLogic) AddVideo(in *usersrpc.AddVideoRequest) (*usersrpc.AddVideoResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User

	if err := findUser.FindUserByUserId(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	findTopic := mysql.Topic{
		Topic_name: in.TopicName,
	}
	err := findTopic.FindTopic(mysql.DB, in.TopicName)
	if err != nil {
		return nil, err
	}
	video := mysql.Video{
		//Model:          gorm.Model{},
		Upload_user_id: int(in.UserId),
		Video_title:    in.VideoTitle,
		Video_category: in.VideoCategory,
		Video_link:     in.VideoLink,
		Cover_link:     in.CoverLink,
		Video_intro:    in.VideoIntro,
		Video_status:   int(in.VideoStatus),
		Topic_id:       int(findTopic.ID),
	}
	err = video.AddVideo(mysql.DB)
	if err != nil {
		return nil, errors.New("发布视频失败")
	}
	return &usersrpc.AddVideoResponse{
		VideoId: int64(video.ID),
	}, nil
}
